package com.pony.iphone.config;

import com.pony.iphone.dto.BaseResponse;
import com.pony.iphone.dto.UsageStats;
import com.pony.iphone.service.impl.QWenChatService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.Map;

@RestController
@RequestMapping("/api/ai")
@Slf4j
public class AIController {
    
    @Autowired
    private QWenChatService qwenChatService;
    
    /**
     * 切换调用方式
     */
    @PostMapping("/switch-method")
    public ResponseEntity<BaseResponse<String>> switchCallMethod(@RequestBody Map<String, Boolean> request) {
        try {
            Boolean useDirect = request.get("useDirect");
            if (useDirect == null) {
                return ResponseEntity.badRequest().body(BaseResponse.error("参数useDirect不能为空"));
            }
            
            qwenChatService.switchToDirectCall(useDirect);
            String message = "已切换到: " + (useDirect ? "直接HTTP调用" : "OpenAiService调用");
            return ResponseEntity.ok(BaseResponse.success(message));
            
        } catch (Exception e) {
            log.error("切换调用方式失败", e);
            return ResponseEntity.status(500).body(BaseResponse.error("切换失败: " + e.getMessage()));
        }
    }
    
    /**
     * 获取当前调用方式信息
     */
    @GetMapping("/method-info")
    public ResponseEntity<BaseResponse<Map<String, Object>>> getMethodInfo() {
        try {
            Map<String, Object> info = qwenChatService.getCallMethodInfo();
            return ResponseEntity.ok(BaseResponse.success("调用方式信息", info));
        } catch (Exception e) {
            return ResponseEntity.status(500).body(BaseResponse.error("获取信息失败: " + e.getMessage()));
        }
    }
    
    /**
     * 获取使用统计
     */
    @GetMapping("/usage-stats")
    public ResponseEntity<BaseResponse<UsageStats>> getUsageStats() {
        try {
            UsageStats stats = qwenChatService.getUsageStats();
            return ResponseEntity.ok(BaseResponse.success("使用统计", stats));
        } catch (Exception e) {
            return ResponseEntity.status(500).body(BaseResponse.error("获取统计失败: " + e.getMessage()));
        }
    }
    
    /**
     * 测试直接调用
     */
    @PostMapping("/test-direct")
    public ResponseEntity<BaseResponse<String>> testDirectCall() {
        try {
            // 临时切换到直接调用
            qwenChatService.switchToDirectCall(true);
            
            // 创建测试请求
            Map<String, Object> testRequest = Map.of(
                "question", "What color is the sky?",
                "answerList", Arrays.asList("Blue", "Green", "Red"),
                "libraryName", "test"
            );
            
            // 这里需要根据您的实际DTO结构调整
            // 实际使用时应该调用正常的聊天接口
            return ResponseEntity.ok(BaseResponse.success("直接调用测试接口准备就绪，请使用 /chat/question 接口测试"));
            
        } catch (Exception e) {
            log.error("直接调用测试失败", e);
            return ResponseEntity.status(500).body(BaseResponse.error("测试失败: " + e.getMessage()));
        }
    }
}